//
//  ftext.swift
//  xzseller
//
//  Created by zhuxietong on 2021/1/27.
//

import Foundation
import Eelay




public enum TextInputType{
    
    case price
    case `default`
    public func filterTextField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        switch self {
        case .price:
            return TextInputControl.priceTextField(textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            return false
        }
    }
    
}

@objc public class TextInputControl:NSObject{
    
    @objc public static func priceTextField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.len > 1{
            let reg = JoReg.priceV.rawValue
            if string.mach(reg){
                textField.text = ""
                return true
            }else{
                return false
            }
        }
        var text = textField.text ?? ""
        if text.contains("."){
            if string.contains("."){
                return false
            }
        }
        
        if text == "0"{
            if("0123456789".contains(string)){
                return false
            }
        }
        if text.len < 1{
            if string.contains("."){
                return false
            }
        }
        if let _ = Regex("\\d+\\.\\d{2,10}$").firstMatch(in: text){
            if("0123456789".contains(string)){
                return false
            }
        }
        if text.hasSuffix("."){
            text = "\(text).0"
        }
        if let v = Double(text){
            if v > 300000{
                if("0123456789".contains(string)){
                    return false
                }
            }
        }
        return true
    }
        
    
}






open class FText:FField,UITextFieldDelegate{
    
    public var textInputType = TextInputType.default
    public let node = MoDisplayNode { () -> UIView in
        let f = UITextField()
        return f
    }
    open var textField:UITextField{
        get{
            return node.view as! UITextField
        }
    }
    open override var valueDisplay:String{
        return "\(value)"
    }
    
    open override func updateDispaly() {
    }
    
    open override func didLoadProps() {
        textField.text = "\(value)"
        textField.ui.font(font: valueStyle.font).text(color: valueStyle.color)
    }
    
    open override func willLayoutRules() {
        super.willLayoutRules()
        textField.delegate = self
        textField.addTarget(self, action: #selector(changeValue), for: .editingChanged)
    }
    
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    @objc open func changeValue(){
        update(value: textField.text ?? "", edit: .input)
    }
   
}

open class FText2:FField, ASEditableTextNodeDelegate{
    
    public var textInputType = TextInputType.default
 
    open var textNode = ASEditableTextNode()
    open override var valueDisplay:String{
        return "\(value)"
    }
    
    open override func updateDispaly() {
    }
    
    open override func didLoadProps() {
        textNode.attributedText = "\(value)".ui.font(font: valueStyle.font).color(color: valueStyle.color).astring
    }
    
    open override func willLayoutRules() {
        super.willLayoutRules()
        textNode.delegate = self
//        textField.delegate = self
        textNode.textView.rx.text.orEmpty.subscribe(onNext:{
            s in
            
        })
    }
    
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    @objc open func changeValue(){
        update(value: textNode.textView.text ?? "", edit: .input)
    }
   
}

